<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/BlogAdmin/layui/css/layui.css" media="all">
</head>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend style="text-align: center">iArchie博客</legend>
</fieldset>
<div style="padding: 20px; background-color: #F2F2F2;">
    <div class="layui-row layui-col-space15" style="height: 300px;">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">用户信息</div>
                <form action="" method="post">
                    <div class="layui-card-body" >
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">文章标签:</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="queryBut" placeholder="请输入需要查询的文章标签"
                                           lay-verify="required" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">文章标题:</label>
                                <div class="layui-input-inline">
                                    <input type="text"  name="queryBut" placeholder="请输入需要查询的文章标题"
                                           lay-verify="required" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">作&nbsp;者:</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="queryBut" placeholder="请输入需要查询的作者"
                                           lay-verify="required" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <button id="btnclick" class="layui-btn layui-btn-warm layui-btn-radius" >点击查询</button>
                        </div>
                        <table id="demo" lay-filter="test"></table>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>


<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit" >编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script src="/BlogAdmin/layui/layui.js"></script>
<script src="/LayuiBlog/js/jquery-min.js"></script>
<script>
    $("#btnclick").click(function () {
        $.ajax({
            type: 'post',
            url: "/admin/deleteBlogByBlogId",
            // contentType: "application/x-www-form-urlencoded",
            dataType: "json",
            data:{
                blogId:data.id
            },
            success: function (res) {
                if (res.result == 1) {
                    layer.msg("删除博客成功！", {icon: 6});
                } else {
                    layer.msg("删除博客失败！", {icon: 5});
                }
            },
            error: function () {
                layer.msg("获取已有分类失败！", {icon: 5});
            }
        })
    })
</script>
<script>
    layui.use('table', function () {
        var table = layui.table;

        //第一个实例
        table.render({
            elem: '#demo'
            , height: 600
            , url: '/admin/getBlogsList' //数据接口
            , page: true //开启分页
            ,request:{
                pageName:'pageNum'
                ,limitName:'pageSize'
            }
            ,parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": 0, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.count, //解析数据长度
                    "data": res.data //解析数据列表
                };
            }
            , cols: [
                        [ //表头
                            {checkbox: true}
                , {field: 'id', title: '文章ID', width: 190, sort: true}
                , {field: 'title', title: '文章标题', width: 340}
                , {field: 'categoryname', title: '文章标签', width: 120}
                , {field: 'firstPicture', title: '文章封面图', width: 120}
                , {field: 'description', title: '文章描述', width: 120}
                , {field: 'username', title: '文章作者', width: 325}
                , {field: 'tagname', title: '文章标签', width: 120}
                , {field: 'updateTime', title: '更新时间', width: 160, sort: true}
                , {
                    fixed: 'right',
                    title: '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;操作',
                    toolbar: '#barDemo',
                    width: 180
                }
                    ]
                ]
        });
        //监听行工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            var tr =obj.tr;
            var layEvent=obj.event;
            //console.log(obj)
            if (layEvent === 'del') {
                layer.confirm('真的删除ID为：'+data.id+"的博客吗？", function (index) {
                    $.ajax({
                        type: 'post',
                        url: "/admin/deleteBlogByBlogId",
                        // contentType: "application/x-www-form-urlencoded",
                        dataType: "json",
                        data:{
                          blogId:data.id
                        },
                        success: function (res) {
                            if (res.result == 1) {
                                layer.msg("删除博客成功！", {icon: 6});
                            } else {
                                layer.msg("删除博客失败！", {icon: 5});
                            }
                        },
                        error: function () {
                            layer.msg("获取已有分类失败！", {icon: 5});
                        }
                    })
                    obj.del();
                    layer.close(index);
                });
            }else if (layEvent === 'edit'){
                layer.open({
                    type: 2,
                    area: ['45%', '600px'],
                    title: '添加分类',
                    skin: 'layui-layer-rim', //加上边框
                    btn: ['保存', '取消'],
                    btnAlign: 'c',
                    top: 100,
                    content: ['/admin/layer_model3.html', 'no'],
                    // content:$("#popUpdateTest") //引用的弹出层的页面层的方式加载修改界面表单
                    success: function (layero,index) {
                        var body=layui.layer.getChildFrame('body',index);
                        body.find("[name=blogId]").val(data.id);
                        body.find("[name=title]").val(data.title);
                        body.find("[name=username]").val(data.username);
                        body.find("[name=categoryname]").val(data.categoryname);
                        body.find("[name=firstPicture]").val(data.firstPicture);
                        body.find("[name=description]").val(data.description);
                    },
                    yes:function (index) {
                        var body=layui.layer.getChildFrame('body',index);
                        var blogId=body.find("[name=blogId]").val();
                        var title=body.find("[name=title]").val();
                        var username=body.find("[name=username]").val();
                        var categoryname=body.find("[name=categoryname]").val();
                        var firstPicture=body.find("[name=firstPicture]").val();
                        var description=body.find("[name=description]").val();
                        var tags=body.find("[name=tags]").val();
                        $.ajax( {
                            url:"/updateBlogByblogId",
                            dataType:"json",
                            type:"POST",
                            data:{
                                blogId:blogId,
                                title:title,
                                username:username,
                                categoryname:categoryname,
                                firstPicture:firstPicture,
                                description:description,
                                tags:tags
                            },
                            success: function (res) {
                                if (res.result == 1) {
                                    obj.update({
                                        blogId:blogId,
                                        title:title,
                                        username:username,
                                        categoryname:categoryname,
                                        firstPicture:firstPicture,
                                        description:description,
                                    });
                                    layer.msg("更新博客文章成功！", {icon: 6});
                                    layer.close(index);
                                } else {
                                    layer.msg("更新博客文章失败！", {icon: 5});
                                }
                            }
                        })
                    }
                });

            }
        });
    });


</script>

</body>
</html>